package com.chen.project.white_jotter.controller;

import com.chen.project.white_jotter.config.SiteProps;
import com.chen.project.white_jotter.pojo.Book;
import com.chen.project.white_jotter.result.Result;
import com.chen.project.white_jotter.service.admin.BookService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/api/admin")
public class AdminBookController {

    @Resource
    private SiteProps siteProps;

    @Resource
    private BookService bookService;

    // 获取一页图书
    @GetMapping("/book")
    public Result getPageBook(int pageNum, int pageSize, String query) {
        System.out.println("请求一页图书");
        System.out.println("页码为" + pageNum);
        System.out.println("一页的大小" + pageSize);
        System.out.println("查询信息为：" + query);
        Map<String, Object> map = bookService.getPageBook(pageNum, pageSize, query);
        return new Result(200, "后台获取图书成功", map);
    }

    // 添加图书
    @PostMapping("book")
    public Result addBook(@RequestBody Book book) {
        System.out.println("添加的图书是："+book);
        bookService.addBook(book);
        return new Result(200, "添加图书");
    }

    // 更新图书信息
    @PutMapping("book")
    public Result updateBook(@RequestBody Book book) {
        System.out.println("需要修改的图书是："+book);
        bookService.updateBook(book);
        return new Result(200, "更新图书信息");
    }

    // 删除图书
    @DeleteMapping("book/{bookId}")
    public Result deleteBook(@PathVariable("bookId") Integer bookId) {
        bookService.deleteBookById(bookId);
        return new Result(200, "删除图书成功");
    }

    // 图书封面上传，保存文件到本地服务器
    // 上传的文件字段名是 file
    @PostMapping("pic")
    public String coversUpload(MultipartFile file) {
        System.out.println("上传的图片名" + file.getName());
        // 服务器保存图片的目录
        String folder = siteProps.getFilePath();

        // 文件原始名
        String originalFilename = file.getOriginalFilename();
        System.out.println("文件原始名：" + originalFilename);
        // 文件类型
        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
        System.out.println("文件类型：" + fileType);
        // 创建File对象
        // UUID重命名
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid + fileType;

        File f = new File(folder + "//" + newFileName);
        System.out.println(f.getAbsoluteFile());

        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }

        try {
            // multi文件转换
            file.transferTo(f);
            // 返回图片在服务器的地址
            String imgURL = siteProps.getRearAddress() + "/api/file/" + f.getName();
            return imgURL;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    // 按书名搜索图书
    @GetMapping("book/search")
    public Result search(@RequestParam("bookName") String bookName) {
        List<Book> books = bookService.search(bookName);
        return new Result(200, "删除图书成功", books);
    }
}
